<template>
  <div class="dashboard-container">
    <h2>System Overview</h2>
    <el-row :gutter="20">

      <!-- 刷新按钮 -->
      <el-button
          class="refresh-btn"
          type="text"
          :icon="Refresh"
          @click="refreshData"/>


      <el-col v-for="card in cardConfigs"
              :key="card.code"
              :span="6"
              class="card-col">
        <el-card shadow="hover" class="dashboard-card">

          <div class="card-content">
            <!-- 图标容器 -->
            <div class="icon-wrapper">
              <component
                  :is="card.icon"
                  :size="36"
                  :style="{ color: card.color }"
              />
            </div>

            <!-- 文字内容 -->
            <div class="text-content">
              <div class="title">{{ getCardData(card.code)?.name || card.defaultName }}</div>
              <div class="number">{{ getCardData(card.code)?.data || '-' }}</div>
            </div>
          </div>

        </el-card>
      </el-col>

    </el-row>


    <el-row :gutter="20" class="m-t-5px">
      <el-col :span="12" :lg="12" :md="12" :sm="24" :xs="24">
        <el-card class="rounded-md" shadow="hover">
          <KoiTimeline1></KoiTimeline1>
        </el-card>
      </el-col>
      <el-col :span="12" :lg="12" :md="12" :sm="24" :xs="24">
        <el-card class="rounded-md" shadow="hover">
          <KoiTimeline2></KoiTimeline2>
        </el-card>
      </el-col>
    </el-row>

  </div>

</template>

<script setup>
import { ref, onMounted } from 'vue'
import { Refresh } from '@element-plus/icons-vue'
import { useDashboardStore } from '@/store/dashboard'
import {ElMessage} from "element-plus";
import KoiTimeline1 from "./components/KoiTimeline1.vue";
import KoiTimeline2 from "./components/KoiTimeline2.vue";


// 卡片固定配置
const cardConfigs = ref([
  {
    code: 'userNumber',
    defaultName: '用户总数', // 默认名称（接口异常时显示）
    icon: 'UserFilled',
    color: '#DC143C'
  },
  {
    code: 'menuNumber',
    defaultName: '菜单数量',
    icon: 'Menu',
    color: '#DC143C'
  },
  {
    code: 'avatar',
    defaultName: '天选之人',
    icon: 'Avatar',
    color: '#DC143C'
  }
])

const dashboardStore = useDashboardStore()

// 获取对应卡片数据
const getCardData = (code) => {
  return dashboardStore.cardData?.find(item => item.code === code)
}

// 刷新数据
const refreshData = async () => {
  try {
    await dashboardStore.listCard({})
  } catch (e) {
    ElMessage.error('数据刷新失败')
  }
}

// 初始化加载
onMounted(() => {
  refreshData()
})
</script>


<style scoped>
.dashboard-container {
  padding: 20px;
  background: #fff;
  border-radius: 4px;
}

.dashboard-card {
  position: relative;
  margin-bottom: 20px;
  transition: transform 0.3s;
}

.dashboard-card:hover {
  transform: translateY(-3px);
}

.refresh-btn {
  position: absolute;
  right: 12px;
  top: 12px;
  padding: 4px;
  color: var(--el-color-info);
  z-index: 1;
}

.refresh-btn:hover {
  color: var(--el-color-primary);
}

.card-content {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 18px 30px 18px 18px;
}

.icon-wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 50px;
}

.text-content {
  flex: 1;
  min-width: 0;
}

.title {
  font-size: 14px;
  color: #909399;
  margin-bottom: 8px;
}

.number {
  font-size: 24px;
  font-weight: 600;
  color: #303133;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}


</style>